function showDiv(val){
		if(document.getElementById("cat_div").style.display=="none"){
			document.getElementById("cat_div").style.display="block";
		}else{
			document.getElementById("cat_div").style.display="none";
		}
	
}

function checkTime(start,end){
	
	var s=new Date(start.replace("-","/")); 
	var e=new Date(end.replace("-","/")); 
    
   if (Date.parse(s)-Date.parse(e)>0) 
   { 
    alert("开始时间不能大于结束时间"); 
    return false; 
   }
	return true;   
	
}

function checkSub(val){
	var type=document.getElementById('offer_type').value;
	var member_level = "a";
	var sending=document.getElementById('spending').value;
	var number=document.getElementById('number').value;
	if (type==1 || type==3 || type==4){
		if(sending==''){
			alert('额满不能为空');
			document.getElementById('spending').focus();
			return false;
		}
		if(parseInt(sending)<0){
			alert('输入金额不能小于0');
			document.getElementById('spending').focus();
			return false;
		}
	}
	
	if(type==2){
		if(number==''){
			alert('量满不能为空');
			document.getElementById('number').focus();
			return false;
		}
		if(parseInt(number)<0){
			alert('输入数量不能小于0');
			document.getElementById('number').focus();
			return false;
		}
	}
	
	if(type==3){
		var discount=document.getElementById('discount').value;
		if(discount==''){
			alert('满额打折，折扣率不能为空');
			document.getElementById('discount').focus();
			return false;
		}else if(parseInt(discount)>100||parseInt(discount)<=0){
			alert('折扣率必须是1-100的数');
			document.getElementById('discount').focus();
			return false;
		}
		
	}
	 
	
	if(type==2){
		if(document.getElementById('send_a_few').value==''){
			alert('送几个不能为空');
			document.getElementById('send_a_few').focus();
			return false;
		}
	}
	if(type==1){
		 
		var pkid_string = judgeCheck();
		if( pkid_string == '' )
		{
			alert('请至少选择一条礼品进行操作!');
			return false;
		}
		document.getElementById('gift_ids').value = pkid_string;
		
	}
	
	if(type==4){
		if(document.getElementById('discount').value==''){
			alert('累计返利比例不能为空');
			document.getElementById('discount').focus();
			return false;
		}
	}
	
	var start=document.getElementById('start_time').value;
	var end=document.getElementById('end_time').value;
	if(start==''){
		alert('开始时间不能为空');
		document.getElementById('start_time').focus();
		return false;
	}
	
	if(end==''){
		alert('结束时间不能为空');
		document.getElementById('end_time').focus();
		return false;
	}
	
	if(!checkTime(start,end)){
		document.getElementById('end_time').focus();
		return false;
	}
	
		
	
	if(val=='addForm'){
		 
				document.addForm.submit();
		 
	}else{
		document.updateForm.submit();
	}
	
}

function search(){
	document.indexForm.submit();
}

function setMember_level(){

}

function clearOrder(val){
	if(val==1){
		document.getElementById("order_amount1").checked="";
		document.getElementById("order_amount2").checked="";
	}else if(val==0){
		document.getElementById("order_amount1").checked="checked";
	}else {
		document.getElementById("good_offer1").checked="";
		document.getElementById("good_offer2").checked="";
	}
}


function setOffer_type(val){
	var type=parseInt(val);
	switch(type){
		case 1:{
				for(var i=0;i<=5;i++){
					document.getElementById("tr"+i).style.display="none";
				}
				document.getElementById("tr6").style.display="";
				document.getElementById("tr7").style.display="";
		}break;
		case 2:{
			document.getElementById("tr0").style.display="";
			document.getElementById("tr1").style.display="";
			document.getElementById("tr3").style.display="";
			document.getElementById("tr2").style.display="none";
			for(var i=4;i<=7;i++){
				document.getElementById("tr"+i).style.display="none";
			}
		}break;
		case 3:{
			document.getElementById("tr0").style.display="";
			document.getElementById("tr2").style.display="";
			document.getElementById("tr3").style.display="";
			document.getElementById("tr1").style.display="none";
			for(var i=4;i<=7;i++){
				document.getElementById("tr"+i).style.display="none";
			}
		}break;
		case 4:{
		    document.getElementById("tr0").style.display="";
			document.getElementById("tr1").style.display="";
			document.getElementById("tr4").style.display="";
			document.getElementById("tr2").style.display="none";
			document.getElementById("tr3").style.display="none";
			for(var i=5;i<=7;i++){
				document.getElementById("tr"+i).style.display="none";
			}
		}break;
		case 5:{
			document.getElementById("tr0").style.display="";
			document.getElementById("tr1").style.display="";
			document.getElementById("tr6").style.display="";
			document.getElementById("tr7").style.display="";
			for(var i=2;i<=5;i++){
				document.getElementById("tr"+i).style.display="none";
			}
		}break;
		case 6:{
			document.getElementById("tr1").style.display="none";
			document.getElementById("tr3").style.display="none";
			document.getElementById("tr4").style.display="none";
			document.getElementById("tr0").style.display="";
			document.getElementById("tr2").style.display="";
			document.getElementById("tr5").style.display="";
			document.getElementById("tr6").style.display="";
			document.getElementById("tr7").style.display="";
		}break;
		default :{
			for(var i=0;i<=5;i++){
					document.getElementById("tr"+i).style.display="none";
				}
				document.getElementById("tr6").style.display="";
				document.getElementById("tr7").style.display="";
		}
	}

}

function setVoucher_type(val){
	var type=document.getElementById('offer_type').value;
	var member_level=document.getElementById('member_level').value;
	if(type==2||type==4||type==5){
		Ti_PaymentRulesInfo.getRuleByParams("","1",member_level,function(data){
			DWRUtil.removeAllOptions('voucher_type');
			DWRUtil.addOptions('voucher_type',{'':'请选择'});
			DWRUtil.addOptions('voucher_type',data);
			selectOptions(val,"voucher_type");
			setNum();
		});
	}else if(type==3||type==6){
		Ti_PaymentRulesInfo.getRuleByParams("","2",member_level,function(data){
			DWRUtil.removeAllOptions('voucher_type');
			DWRUtil.addOptions('voucher_type',{'':'请选择'});
			DWRUtil.addOptions('voucher_type',data);
			selectOptions(val,"voucher_type");
			setNum();
			});
	}else{
		Ti_PaymentRulesInfo.getRuleByParams("","3","",function(data){
			DWRUtil.removeAllOptions('voucher_type');
			DWRUtil.addOptions('voucher_type',{'':'请选择'});
			DWRUtil.addOptions('voucher_type',data);
			selectOptions(val,"voucher_type");
			setNum();
			});
	}
	
	
}

function selectOptions(obj,id){ 
    var selectOption=$(id); 
    if(id=='gift_ids'){
    	for(var i=0;i<selectOption.length;i++){   
            if(obj==selectOption[i].value){   
                selectOption[i].selected=true;   
            }   
        }   
    }else{
    	for(var i=0;i<selectOption.length;i++){   
            if(obj==selectOption[i].value){   
                selectOption[i].selected=true;   
                break;   
            }   
        }   
    }
    }  


function setNum(){
	var val=document.getElementById("voucher_type");
	var num=subStr(val);
	if(isNaN(num))num=0;
	document.getElementById("voucher_num").value=num;
}

function setGifts(val) {
	var str="";
	str=setClassStr();
	Ti_giftInfo.getGiftsByClassAttr(str,function(data){
			DWRUtil.removeAllOptions('gift_ids');
			DWRUtil.addOptions('gift_ids',{'':'请选择'});
			DWRUtil.addOptions('gift_ids',data);
			selectOptions(val,"gift_ids");
	});
} 

function setClassStr(){
	var sort1="";
	sort1=document.getElementById('sort1').value;
	var sort2="";
	sort2=document.getElementById('sort2').value;
	var str="";
	if(sort1=='') return str;
	if(sort2=='') str=sort1+"|";
	else str=sort1+"|"+sort2+"|";
	return str;
}

function clearStr(val){
	var type=document.getElementById("offer_type").value;
	if(val==1){
		//if(document.getElementById("spending").value!='')
			document.getElementById("spending").value="";
	}else if(val==0){
		//if(document.getElementById("number").value!='')
			document.getElementById("number").value="";
	}else if(val==3){
		if(type==2||type==3){
			alert("使用打折，则不能优惠运费");
			document.getElementById("ship_offer").value="";
		}
	}else if(val==6){
		if(type!=6){
			alert("使用买几送几优惠才可送多个礼物");
			document.getElementById("send_a_few").value="";
		}
	}
}

function subStr(val){
	var str="";
	var num=0;
   for(i=0;i<val.length;i++){   
                if(val.options[i].selected){
                	str=val.options[i].text;
                	num +=parseInt(str.substring(str.indexOf("|")+1,str.length));
                }
            }
       return num;
}


function giftsStocknum(){
			var gifts="";
			var str="";
			var stocknum=0;
			var val=document.getElementById("gift_ids");
            for(i=0;i<val.length;i++){   
                if(val.options[i].selected){
                	str=val.options[i].text;
                	stocknum +=parseInt(str.substring(str.indexOf("|")+1,str.length));
                }
            }
       return stocknum;
}

function checkNumber(){
	var type=document.getElementById("offer_type").value;
	var send_a_few=document.getElementById("send_a_few").value;
	
	if(type==6){
		if(send_a_few==''){
			alert("请选择您要送几个礼物");
			document.getElementById('send_a_few').focus();
			return false;
		}
	}
		var str=giftsStocknum();
		if(str==0){
			alert("您还没有选择礼品");
			return false;
		}	
		if(send_a_few>str){
			alert("您送出的礼物已超过库存量");
			document.getElementById('gift_ids').focus();
			return false;
		}
	return true;
}


function judgeCheck()
{
    var pkid_string = '';
		var listsize = document.getElementById('listsize').value;
		for(var i=0;i<listsize;i++)
		{
				if(document.getElementById('checkone'+i).checked)
				{
					pkid_string += document.getElementById('checkone'+i).value+'|';
				}
		}
    return pkid_string;
}


